package com.bwf.lgqingdan.gui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bwf.lgqingdan.R;
import com.bwf.lgqingdan.entity.ArticlesBean;
import com.bwf.lgqingdan.gui.adapter.TagDetailsArticlesRecycleViewAdapter;
import com.bwf.lgqingdan.gui.adapter.base.BaseRecycleViewAdapter;
import com.bwf.lgqingdan.mvp.presenter.SearchArticleListPresenter;
import com.bwf.lgqingdan.mvp.presenter.impl.SearchArticleListPresenterImpl;
import com.bwf.lgqingdan.mvp.view.SearchDataListView;
import com.bwf.lgqingdan.utils.IntentUtils;
import com.bwf.lgqingdan.utils.KeyBoardUtils;
import com.cjj.MaterialRefreshLayout;
import com.cjj.MaterialRefreshListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/11/20.
 * 搜索文章
 */

public class SearchArticleListFragment extends FragmentBase implements SearchDataListView<ArticlesBean> {
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.refreshLayout)
    MaterialRefreshLayout refreshLayout;
    private static final String TAG_KEYWORD = "keyword";
    @BindView(R.id.textView_tell_us)
    TextView textViewTellUs;
    @BindView(R.id.search_result_no_item_root_view)
    RelativeLayout searchResultNoItemRootView;
    private String keyword;

    private SearchArticleListPresenter presenter;
    private TagDetailsArticlesRecycleViewAdapter articleAdapter;
    LinearLayoutManager linearLayoutManager;

    public static SearchArticleListFragment newInstance(String keyword) {
        SearchArticleListFragment fragment = new SearchArticleListFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TAG_KEYWORD, keyword);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected int getContentViewResID() {
        return R.layout.fragment_search_article_list;
    }

    @Override
    protected void initViews() {
        linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recycleView.setLayoutManager(linearLayoutManager);

        articleAdapter = new TagDetailsArticlesRecycleViewAdapter(getActivity());
        articleAdapter.setOnItemClickListener(new BaseRecycleViewAdapter.OnItemClickListener<ArticlesBean>() {
            @Override
            public void onItemClick(int position, View view, ArticlesBean data) {
                clickItem(data);
            }
        });
        recycleView.setAdapter(articleAdapter);

        refreshLayout.setMaterialRefreshListener(new MaterialRefreshListener() {
            @Override
            public void onRefresh(MaterialRefreshLayout materialRefreshLayout) {
                presenter.loadData(keyword);
            }
        });
        recycleView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                KeyBoardUtils.closeKeybord((EditText) getActivity().findViewById(R.id.editText_search), getActivity());
                return false;
            }
        });
    }

    @Override
    protected void initDatas() {
        keyword = getArguments().getString(TAG_KEYWORD, "");
        presenter = new SearchArticleListPresenterImpl(this);
        presenter.loadData(keyword);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        ButterKnife.bind(this, rootView);
        return rootView;
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {
        refreshLayout.finishRefresh();
    }

    @Override
    public void showData(List<ArticlesBean> datas) {
        linearLayoutManager.scrollToPosition(0);
        articleAdapter.setDatas(datas);
        refreshLayout.finishRefresh();
    }

    @Override
    public void showLoadFailed(String failedMsg) {

    }

    @Override
    public void clickItem(ArticlesBean data) {
        IntentUtils.gotoArticleDetailActivity(getActivity(), data.getId());
    }

    @Override
    public void clickTellus() {
        IntentUtils.gotoFeedbackActivity(getActivity());
    }

    @Override
    public void showNoDataLayout() {
        searchResultNoItemRootView.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideNoDataLayout() {
        searchResultNoItemRootView.setVisibility(View.GONE);
    }

    @Override
    public void clearListDatas() {
        articleAdapter.clearDatas();
    }

    public void refresh(String keyword) {
        this.keyword = keyword;
        presenter.loadData(keyword);
    }

    @OnClick(R.id.textView_tell_us)
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.textView_tell_us:
                clickTellus();
                break;
        }
    }
}
